package swing;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import model.EditADialogLogic;
import tableDancer.ListeA;
import tableDancer.Revierinfo;
import tableDancer.Todesart;

@SuppressWarnings("serial")
public class EditADialog
extends JDialog
implements SwingInterface{

	private JLabel l_tierkat, l_tierA, l_datum, l_gewicht, l_bemerkungA, l_todesart;
	public JComboBox cb_todesart;
	public JTextField tf_tierkat, tf_tierA, tf_datum, tf_gewicht, tf_bemerkungA;
	private JButton bt_save, bt_abort;

	private GridBagLayout gbl_listA;
	private GridBagConstraints gbc_listA;
	private JPanel p_centerA, p_southA;

	private EditADialogLogic eadl;
	public ListeA la;
	public Revierinfo ri_akt;
	public ListADialog lad;

	public EditADialog(Object la, Revierinfo ri_akt, ListADialog lad){
		this.la = (ListeA) la;
		this.ri_akt = ri_akt;
		this.lad = lad;

		setTitle("Eintrag ListeA editieren");
		setModal(true);
		setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);

		setInitiate();
		setLayout();
		setListener();
	}

	public void setInitiate() {

		eadl = new EditADialogLogic(this);

		gbl_listA = new GridBagLayout();
		gbc_listA = new GridBagConstraints();
		gbc_listA.fill = GridBagConstraints.BOTH;
		gbc_listA.insets = new Insets(4,4,4,4);

		p_centerA = new JPanel();
		p_southA = new JPanel();


		l_tierkat = new JLabel("Wildart");
		l_tierA = new JLabel("Wildklasse");
		l_gewicht = new JLabel("Gewicht in Kilogramm");
		l_datum = new JLabel("Datum des Abschusses");
		l_bemerkungA = new JLabel("Bemerkung");
		l_todesart = new JLabel("Todesart");

		Object[] temp = {Todesart.ERLEGT, Todesart.FALLWILD};
		cb_todesart = new JComboBox(temp);
		if(la.getTodesart() == Todesart.ERLEGT){
			cb_todesart.setSelectedItem(temp[0]);
		}else{
			cb_todesart.setSelectedItem(temp[1]);
		}
		System.out.println(la.getTier_a_name());
		System.out.println(la.getList_a_id());

		tf_tierkat = new JTextField(eadl.getTierKat(), 20);
		tf_tierkat.setEditable(false);
		tf_tierA = new JTextField (la.getTier_a_name());
		tf_tierA.setEditable(false);
		tf_bemerkungA = new JTextField(la.getBemerkung());
		tf_gewicht = new JTextField(String.valueOf(la.getGewicht()));
		tf_datum = new JTextField(la.getDatum());

		bt_save = new JButton("Speichern");
		bt_abort = new JButton("Abbrechen");

	}

	public void setLayout() {

		setLayout(new BorderLayout());
		add(p_centerA, BorderLayout.CENTER);
		p_centerA.setLayout(gbl_listA);
		addToGridA(0,0,1,1,l_tierkat);
		addToGridA(1,0,1,1,tf_tierkat);
		addToGridA(0,1,1,1,l_tierA);
		addToGridA(1,1,1,1,tf_tierA);
		addToGridA(0,2,1,1,l_datum);
		addToGridA(1,2,1,1,tf_datum);
		addToGridA(0,3,1,1,l_gewicht);
		addToGridA(1,3,1,1,tf_gewicht);
		addToGridA(0,4,1,1,l_todesart);
		addToGridA(1,4,1,1,cb_todesart);
		addToGridA(0,5,1,1,l_bemerkungA);
		addToGridA(1,5,1,1,tf_bemerkungA);
		add(p_southA, BorderLayout.SOUTH);
		p_southA.add(bt_save);
		p_southA.add(bt_abort);

	}

	public void setListener() {
		bt_save.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				eadl.save();
			}
		});

		bt_abort.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				eadl.closeDialog();
			}
		});
	}

	private void addToGridA(int gridx, int gridy, int gridwidth, int gridheight, Component comp)
	{
		gbc_listA.gridx = gridx;
		gbc_listA.gridy = gridy;
		gbc_listA.gridheight = gridheight;
		gbc_listA.gridwidth = gridwidth;
		gbl_listA.setConstraints(comp, gbc_listA);
		p_centerA.add(comp);
	}

	public void start() {
		setLocation(320,320);
		pack();
		setResizable(false);
		setVisible(true);
	}

}
